<template>
	<view>
		<uni-section title="活动提醒" type="line">
			<uni-list>
				<uni-list-chat
					v-for="reminder in reminderList"
					:title="reminder.activityName"
					:avatar="reminder.pic"
					:time="reminder.time"
					:note="reminder.content"
					badge-positon="left"
					:badge-text="reminder.num" 
					:to="`/pages/message-list/message-list?activityId=${reminder.activityId}&uid=${uid}&type=reminder`"
				></uni-list-chat>
			</uni-list>
			<uni-load-more iconType="circle" :status="reminderStatus" @clickLoadMore="getMoreReminder" />
		</uni-section>
		<uni-section title="日程提醒" type="line">
			<uni-list>
			<uni-list-chat
				v-for="schedule in scheduleList"
				:time="schedule.time"
				:note="schedule.content"
			></uni-list-chat>
			</uni-list>
			<uni-load-more iconType="circle" :status="scheduleStatus" @clickLoadMore="getMoreSchedule" />
		</uni-section>
		<uni-section title="活动变动" type="line">
			<uni-list>
				<uni-list-chat
					v-for="change in changeList"
					:title="change.activityName"
					:avatar="change.pic"
					:time="change.time"
					:note="change.content"
					badge-positon="left"
					:badge-text="change.num"
					:to="`/pages/message-list/message-list?activityId=${change.activityId}&uid=${uid}&type=change`"
				></uni-list-chat>
			</uni-list>
			<uni-load-more iconType="circle" :status="changeStatus" @clickLoadMore="getMoreChange" />
		</uni-section>
		<uni-section title="评论消息" type="line">
			<template v-slot:right>
				<button style="font-size: 18rpx;" type="default" @click="changeCommentFilter">{{ commentBtnText }}</button>
			</template>
			<uni-list>
				<uni-list-chat
					v-for="comment in commentList"
					:title="comment.activityName"
					:avatar="comment.pic"
					:time="comment.time"
					:note="`${comment.userName}回复了你:${comment.content}`"
					badge-positon="left"
					:badge-text="comment.badge"
					:to="`/pages/activityDetail/activityDetail?id=${comment.activityId}&mid=${comment.id}`"
				></uni-list-chat>
			</uni-list>
			<uni-load-more iconType="circle" :status="commentStatus" @clickLoadMore="getMoreComment" />
		</uni-section>
		<uni-section title="审核消息" type="line">
			<template v-slot:right>
				<button style="font-size: 18rpx;" type="default" @click="changeCheckFilter">{{ checkBtnText }}</button>
			</template>
			<uni-list>
			<uni-list-chat
				v-for="check in checkList"
				:title="check.activityName"
				:avatar="check.pic"
				:time="check.time"
				:note="check.content"
				badge-positon="left"
				:badge-text="check.badge"
				:to="check.src"
			></uni-list-chat>
			</uni-list>
			<uni-load-more iconType="circle" :status="checkStatus" @clickLoadMore="getMoreCheck" />
		</uni-section>
	</view>
</template>

<script>
export default {
	data() {
		return {
			uid: 1,
			apiUrl: 'http://localhost:8080/',

			reminderStatus: 'more',
			reminderList: [],
			reminderPageNum: 1,
			reminderPageSize: 5,
			
			scheduleStatus: 'more',
			scheduleList: [],
			schedulePageNum: 1,
			schedulePageSize: 5,

			changeStatus: 'more',
			changeList: [],
			changePageNum: 1,
			changePageSize: 5,

			commentStatus: 'more',
			commentList: [],
			commentPageNum: 1,
			commentPageSize: 5,
			commentBtnText: '只看未读',
			
			checkStatus: 'more',
			checkList: [],
			checkPageNum: 1,
			checkPageSize: 5,
			checkBtnText: '只看未读',
			
			verifyPath:""
		};
	},
	onLoad() {
		this.uid = uni.getStorageSync("uid")
		uni.startPullDownRefresh();
	},
	onShow() {
		uni.startPullDownRefresh();
	},
	onPullDownRefresh() {
		this.reminderPageNum = 1;
		this.getReminderList();
		
		this.schedulePageNum = 1;
		this.getScheduleList();

		this.changePageNum = 1;
		this.getChangeList();

		this.commentPageNum = 1;
			if (this.commentBtnText == '只看未读') {
				this.getCommentList();
			} else {
				this.getUnreadCommentList();
			}
		
		this.checkPageNum = 1;
		if (this.checkBtnText == '只看未读') {
			this.getCheckList();
		} else {
			this.getUnreadCheckList();
		}
		
		uni.stopPullDownRefresh();
	},
	methods: {
		// setVerfiy(data){
		// 	if(data.state == "withdraw")
		// 	{
		// 		uni.navigateTo({
		// 			url:"/pages/changeActivity/changeActivity?aid=" + data.id
		// 		})
		// 	}
		// 	else{
		// 		uni.navigateTo({
		// 			url:"/pages/activityDetail/activityDetail?aid="+data.id
		// 		})
		// 	}
		// },
		getMoreReminder() {
			this.reminderPageNum++;
			this.getReminderList();
		},
		getReminderList() {
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'message/outer',
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: {
					type: 'reminder',
					toUid: this.uid,
					pageNum: this.reminderPageNum,
					pageSize: this.reminderPageSize
				},
				success: res => {
					console.log(res.data);
					if (this.reminderPageNum <= 1) {
						this.reminderList = [];
					}
					let originSize = this.reminderList.length;
					this.reminderList.push.apply(this.reminderList, res.data.records);
					let deltaSize = res.data.records.length;
					if (res.data.total <= originSize + deltaSize) {
						this.reminderStatus = 'noMore';
					} else {
						this.reminderStatus = 'more';
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		getMoreSchedule() {
			this.schedulePageNum++;
			this.getScheduleList();
		},
		getScheduleList() {
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'message/single',
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: {
					type: 'schedule',
					toUid: this.uid,
					pageNum: this.schedulePageNum,
					pageSize: this.schedulePageSize
				},
				success: res => {
					console.log(res.data);
					if (this.schedulePageNum <= 1) {
						this.scheduleList = [];
					}
					let originSize = this.scheduleList.length;
					this.scheduleList.push.apply(this.scheduleList, res.data.records);
					let deltaSize = res.data.records.length;
		
					if (res.data.total <= originSize + deltaSize) {
						this.scheduleStatus = 'noMore';
					} else {
						this.scheduleStatus = 'more';
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		getMoreChange() {
			this.changePageNum++;
			this.getChangeList();
		},
		getChangeList() {
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'message/outer',
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: {
					type: 'change',
					toUid: this.uid,
					pageNum: this.changePageNum,
					pageSize: this.changePageSize
				},
				success: res => {
					console.log(res.data);
					if (this.changePageNum <= 1) {
						this.changeList = [];
					}
					let originSize = this.changeList.length;
					this.changeList.push.apply(this.changeList, res.data.records);
					let deltaSize = res.data.records.length;
					if (res.data.total <= originSize + deltaSize) {
						this.changeStatus = 'noMore';
					} else {
						this.changeStatus = 'more';
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		getMoreComment() {
			this.commentPageNum++;
			if (this.commentBtnText == '只看未读') {
				this.getCommentList();
			} else {
				this.getUnreadCommentList();
			}
		},
		getCommentList() {
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'message/single',
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: {
					type: 'comment',
					toUid: this.uid,
					pageNum: this.commentPageNum,
					pageSize: this.commentPageSize
				},
				success: res => {
					console.log(res.data);
					if (this.commentPageNum <= 1) {
						this.commentList = [];
					}
					let originSize = this.commentList.length;
					this.commentList.push.apply(this.commentList, res.data.records);
					let deltaSize = res.data.records.length;
					for (let i = originSize; i < originSize + deltaSize; i++) {
						if (this.commentList[i].isRead == 1) {
							this.commentList[i].badge = '';
						} else {
							this.commentList[i].badge = 'dot';
						}
					}

					if (res.data.total <= originSize + deltaSize) {
						this.commentStatus = 'noMore';
					} else {
						this.commentStatus = 'more';
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		getUnreadCommentList() {
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'message/single',
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: {
					type: 'comment',
					toUid: this.uid,
					isRead: 0,
					pageNum: this.commentPageNum,
					pageSize: this.commentPageSize
				},
				success: res => {
					console.log(res.data);
					if (this.commentPageNum <= 1) {
						this.commentList = [];
					}
					let originSize = this.commentList.length;
					this.commentList.push.apply(this.commentList, res.data.records);
					let deltaSize = res.data.records.length;
					for (let i = originSize; i < originSize + deltaSize; i++) {
						if (this.commentList[i].isRead == 1) {
							this.commentList[i].badge = '';
						} else {
							this.commentList[i].badge = 'dot';
						}
					}

					if (res.data.total <= originSize + deltaSize) {
						this.commentStatus = 'noMore';
					} else {
						this.commentStatus = 'more';
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		changeCommentFilter() {
			if (this.commentBtnText == '只看未读') {
				this.commentBtnText = '显示所有';
				this.commentPageNum = 1;
				this.getUnreadCommentList();
			} else {
				this.commentBtnText = '只看未读';
				this.commentPageNum = 1;
				this.getCommentList();
			}
		},
		getMoreCheck() {
			this.checkPageNum++;
			if (this.checkBtnText == '只看未读') {
				this.getCheckList();
			} else {
				this.getUnreadCheckList();
			}
		},
		getCheckList() {
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'message/single',
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: {
					type: 'check',
					toUid: this.uid,
					pageNum: this.checkPageNum,
					pageSize: this.checkPageSize
				},
				success: res => {
					console.log(res.data);
					if (this.checkPageNum <= 1) {
						this.checkList = [];
					}
					let originSize = this.checkList.length;
					this.checkList.push.apply(this.checkList, res.data.records);
					let deltaSize = res.data.records.length;
					for (let i = originSize; i < originSize + deltaSize; i++) {
						if (this.checkList[i].isRead == 1) {
							this.checkList[i].badge = '';
						} else {
							this.checkList[i].badge = 'dot';
						}
						
						if(this.checkList[i].state == 'withdraw'){
							this.checkList[i].src = "/pages/changeActivity/changeActivity?aid=" + this.checkList[i].activityId+"&mid="+this.checkList[i].id}
						else{
							this.checkList[i].src = "/pages/activityDetail/activityDetail?id=" + this.checkList[i].activityId+"&mid="+this.checkList[i].id
						}
						
					}
		
					if (res.data.total <= originSize + deltaSize) {
						this.checkStatus = 'noMore';
					} else {
						this.checkStatus = 'more';
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		getUnreadCheckList() {
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'message/single',
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: {
					type: 'check',
					toUid: this.uid,
					isRead: 0,
					pageNum: this.checkPageNum,
					pageSize: this.checkPageSize
				},
				success: res => {
					console.log(res.data);
					if (this.checkPageNum <= 1) {
						this.checkList = [];
					}
					let originSize = this.checkList.length;
					this.checkList.push.apply(this.checkList, res.data.records);
					let deltaSize = res.data.records.length;
					for (let i = originSize; i < originSize + deltaSize; i++) {
						if (this.checkList[i].isRead == 1) {
							this.checkList[i].badge = '';
						} else {
							this.checkList[i].badge = 'dot';
						}
					}
		
					if (res.data.total <= originSize + deltaSize) {
						this.checkStatus = 'noMore';
					} else {
						this.checkStatus = 'more';
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		changeCheckFilter() {
			if (this.checkBtnText == '只看未读') {
				this.checkBtnText = '显示所有';
				this.checkPageNum = 1;
				this.getUnreadCheckList();
			} else {
				this.checkBtnText = '只看未读';
				this.checkPageNum = 1;
				this.getCheckList();
			}
		}
	}
};
</script>

<style></style>
